package org.lts.yuqueexportor.config;

import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * HTTP客户端配置
 */
@Configuration
public class HttpClientConfig {

    @Bean
    public CloseableHttpClient httpClient(YuqueConfig yuqueConfig) {
        // 配置请求参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.STANDARD) // 使用标准Cookie规范,忽略无效Cookie
                .setConnectTimeout(yuqueConfig.getDownload().getTimeout())
                .setSocketTimeout(yuqueConfig.getDownload().getTimeout())
                .setConnectionRequestTimeout(yuqueConfig.getDownload().getTimeout())
                .build();

        // 创建HttpClient
        return HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
                .build();
    }
}
